package statistics

import (
	"context"
	"sale-admin/internal/app/web/service/robot"

	"gorm.io/gorm"
)

type CreatePayStatistics struct{}

type FaceCountAndGoodsClickReturnStruct struct {
	Date      string  `json:"date"`
	FaceCount float64 `json:"face_count"`
	ClickNum  float64 `json:"click_num"`
}
type CreateAndPayReturnStruct struct {
	Date        string  `json:"date"`
	CreateCount float64 `json:"create_count"`
	PayCount    float64 `json:"pay_count"`
}

func (cps CreatePayStatistics) GetCreateAndPay(ctx context.Context, db *gorm.DB, merchantID string, side, Type int, shopIDList []string, areas1,
	areas2, areas3, areas5, salesID, startTime, endTime string) (data interface{}, err error) {
	if side == 2 {
		//左侧 曝光与点击
		var FaceCountAndGoodsClickReturn = []FaceCountAndGoodsClickReturnStruct{}
		GoodsClickList, _ := GoodsClickStatistics{}.GetGoodsClick(ctx, db, merchantID, startTime, endTime, salesID, shopIDList, Type)
		FaceCountList, _, _ := robot.FaceCount{}.GetFaceCount(ctx, db, merchantID, areas3, startTime, endTime, salesID, shopIDList, Type, false)

		for _, faceCount := range FaceCountList {
			for _, GoodsClick := range GoodsClickList {
				if GoodsClick.Date == faceCount.Date {
					FaceCountAndGoodsClickReturn = append(FaceCountAndGoodsClickReturn, FaceCountAndGoodsClickReturnStruct{
						Date:      faceCount.Date,
						FaceCount: faceCount.FaceCount,
						ClickNum:  GoodsClick.ClickNum,
					})
				}
			}
		}
		return FaceCountAndGoodsClickReturn, nil
	} else {
		//右侧 创单与支付
		var CreateOrderCount = []OrderCountReturnStruct{}
		var PayOrderCount = []OrderCountReturnStruct{}
		var CreateAndPayReturn = []CreateAndPayReturnStruct{}
		CreateOrderStatus := "1,3,4,6"
		PayOrderStatus := "4"
		CreateOrderCount, _ = OrderCountStatistic{}.GetOrderCount(ctx, db, merchantID, CreateOrderStatus,
			areas1, areas2, areas3, areas5, startTime, endTime, salesID, shopIDList, Type)
		PayOrderCount, _ = OrderCountStatistic{}.GetOrderCount(ctx, db, merchantID, PayOrderStatus,
			areas1, areas2, areas3, areas5, startTime, endTime, salesID, shopIDList, Type)
		for _, createOrder := range CreateOrderCount {
			for _, payOrder := range PayOrderCount {
				if createOrder.Date == payOrder.Date {
					CreateAndPayReturn = append(CreateAndPayReturn, CreateAndPayReturnStruct{
						Date:        createOrder.Date,
						CreateCount: createOrder.CountNum,
						PayCount:    payOrder.CountNum,
					})
				}
			}
		}
		return CreateAndPayReturn, nil
	}
}
